package com.wyy.treeview.ui;

import java.util.ArrayList;

import com.wyy.treeview.R;
import com.wyy.treeview.R.drawable;
import com.wyy.treeview.R.id;
import com.wyy.treeview.R.layout;

import android.content.Context;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class TreeViewAdapter extends BaseAdapter {
	/** 元素数据源 */
	private ArrayList<TreeElement> elementsData;
	/** 树中元素 */
	private ArrayList<TreeElement> elements;
	/** item的行首缩进基数 */
	private int indentionBase;
	private Context context;
	private int selectedPosition = -1;// 选中的位置

	public TreeViewAdapter(Context context, ArrayList<TreeElement> elements,
			ArrayList<TreeElement> elementsData) {
		this.elements = elements;
		this.elementsData = elementsData;
		this.context = context;
		indentionBase = 60;
	}

	public ArrayList<TreeElement> getElements() {
		return elements;
	}

	public ArrayList<TreeElement> getElementsData() {
		return elementsData;
	}

	@Override
	public int getCount() {
		return elements.size();
	}

	@Override
	public Object getItem(int position) {
		return elements.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	public void setSelectedPosition(int selectedPosition) {
		this.selectedPosition = selectedPosition;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = View.inflate(context, R.layout.list_item_tree, null);
			holder.imageView = (ImageView) convertView
					.findViewById(R.id.display_imageview);
			holder.textView = (TextView) convertView
					.findViewById(R.id.content_textview);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		TreeElement element = elements.get(position);
		int level = element.getLevel();
		holder.imageView.setPadding(indentionBase * level,
				holder.imageView.getPaddingTop(),
				holder.imageView.getPaddingRight(),
				holder.imageView.getPaddingBottom());  //设置缩进量
		holder.textView.setText(element.getContentText());

		if (element.isHasChildren() && !element.isExpanded()) {	//有子节点且收缩
			holder.imageView.setImageResource(R.drawable.icon_close);
			holder.imageView.setVisibility(View.VISIBLE);
		} else if (element.isHasChildren() && element.isExpanded()) {//有子节点且展开
			holder.imageView.setImageResource(R.drawable.icon_open);
			holder.imageView.setVisibility(View.VISIBLE);
		} else if (!element.isHasChildren()) { //无子节点
			holder.imageView.setImageResource(R.drawable.icon_location);
			holder.imageView.setVisibility(View.VISIBLE);
		}

		//下标与选择项相同时，更改字体颜色
		if (position == selectedPosition) {
			holder.textView.setTextColor(Color.parseColor("#FF3F9FE0"));
		} else {
			holder.textView.setTextColor(Color.parseColor("#FF000000"));
		}
		return convertView;
	}

	static class ViewHolder {
		ImageView imageView;
		TextView textView;
	}
}
